﻿using System;
using System.Collections;
using System.Collections.Generic;
using Spine.Unity;
using UnityEngine;
using AnimationState = Spine.AnimationState;
public class SpineObstacle : MonoBehaviour
{
	/// <summary>
	/// SkeletonAnimation
	/// </summary>
	private SkeletonAnimation _SkeletonAnimation;

	/// <summary>
	/// SpineBomeTransform
	/// </summary>
	public Transform spineBomeTransform;

	/// <summary>
	/// mUnRegisterEventActions
	/// </summary>
	private List<Action> mUnRegisterEventActions = new List<Action>();
	/// <summary>
	/// Awake
	/// </summary>
	private void Awake()
	{
		_SkeletonAnimation = spineBomeTransform.GetComponent<SkeletonAnimation>();
		spineBomeTransform.gameObject.SetActive(false);
	}
	
	/// <summary>
	/// 爆炸效果
	/// </summary>
	/// <param name="action"></param>
	public void ShowBoom(Action action=null)
    {
        if (_SkeletonAnimation == null)
        {
            Debug.LogError("_SkeletonAnimation == null");
            return;
        }

        spineBomeTransform.gameObject.SetActive(true);
        _SkeletonAnimation.AnimationState.SetAnimation(0, "animation", false);
        AnimationState.TrackEntryDelegate ac = delegate
        {
            if (action != null)
            {
                action();
            }
        };

        _SkeletonAnimation.AnimationState.Complete += ac;
        mUnRegisterEventActions.Add(() =>
        {
            _SkeletonAnimation.AnimationState.Complete -= ac;
        });
    }
	
	/// <summary>
    /// 移除所有回调事件
    /// </summary>
    public void UnRegisterAll()
    {
        mUnRegisterEventActions.ForEach(action => action());
        mUnRegisterEventActions.Clear();
    }

    /// <summary>
    /// OnDestroy
    /// </summary>
    private void OnDestroy()
    {
        UnRegisterAll();
    }
	
	
	
	
	
}
